package 算法.NiuKe.TB;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:谢君臣
 * @Date:2021/5/4-9:27
 * @version:1.0 以字符串的形式读入两个数字，编写一个函数计算它们的乘积，以字符串形式返回。
 * （字符串长度不大于10000，保证字符串仅由'0'~'9'这10种字符组成）
 * @function:
 */
public class NC8 {
    public static void main(String[] args) {
        System.out.println(solve("1234231", "2234234234"));
    }
    public static String solve (String s, String t) {
        // write code here
        List<String> num = new ArrayList<>();
        for (int i = 1; i <= t.length(); i++) {
            num.add(getNum(s,t.charAt(t.length()-i)-'0',i-1));
        }
        return sumNum(num);
    }

    private static String sumNum(List<String> num) {
        StringBuilder res = new StringBuilder(num.get(0));
        for (int i = 1; i < num.size(); i++) {
            StringBuilder temp = new StringBuilder(num.get(i));
            res = getResult(res,temp);
        }
        return res.toString();
    }

    private static StringBuilder getResult(StringBuilder res, StringBuilder temp) {
        int i = 1,tage = 0;
        StringBuilder s = new StringBuilder();
        while (i<=res.length()&&i<=temp.length()){
            int num = res.charAt(res.length()-i)-'0'+ temp.charAt(temp.length()-i)-'0'+tage;
            s.append(num% 10);
            tage = num/10;
            i++;
        }
        while (i<=res.length()){
            int num = res.charAt(res.length()-i)-'0'+ tage;
            s.append(num% 10);
            tage = num/10;
            i++;
        }
        while (i<=temp.length()){
            int num = temp.charAt(temp.length()-i)-'0'+ tage;
            s.append(num% 10);
            tage = num/10;
            i++;
        }
        if (tage>0)s = s.append(tage);
        return s.reverse();
    }

    private static String getNum(String s, int n,int t) {
        StringBuilder res = new StringBuilder();
        int tage = 0;
        for (int i = 1; i <= s.length(); i++) {
            int temp = (s.charAt(s.length()-i)-'0') * n + tage;
            res.append(temp%10);
            tage = temp/10;
        }
        if (tage>0)res.append(tage);
        res.reverse();
        while (t>0){res.append('0');t--;}
        return res.toString();
    }
}
